package com.xbongbong.pro.message.pushset;/*/*/

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.message.domain.entity.PushSetEntity;
import com.xbongbong.pro.message.base.AbstractPushSet;
import com.xbongbong.pro.message.constant.PushSetConstant;
import com.xbongbong.pro.message.enums.PushObjectTypeEnum;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.model.PushSetModel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.Getter;
import lombok.Setter;

/**
 * 发票消息设置
 *
 * @author GuoJun.HU
 * @since V1.0
 * @date 2019/2/19 10:44
 */
@Getter
@Setter
public class InvoicePushSet extends AbstractPushSet {
    private String pushObject;

    @Override
    public PushSetEntity toPushSetEntity(String pushObject, String pushTime, String attrEntity) {
        setPushObject(pushObject);
        PushSetEntity pushSetEntity = new PushSetEntity();
        pushSetEntity.setAddTime(addTime);
        pushSetEntity.setAttrEntity(attrEntity);
        pushSetEntity.setCorpid(corpid);
        pushSetEntity.setDel(del);
        pushSetEntity.setEnable(enable);
        pushSetEntity.setId(id);
        pushSetEntity.setPushObject(pushObject);
        pushSetEntity.setPushTime(pushTime);
        pushSetEntity.setRefEntity(refEntity);
        pushSetEntity.setSubType(subType);
        pushSetEntity.setType(type);
        pushSetEntity.setUpdateTime(updateTime);
        return pushSetEntity;
    }

    @Override
    public PushSetEntity getPushSet(String corpid, Integer subType, PushSetModel pushSetModel) {
        Integer type = PushTypeEnum.INVOICE_PUSH.getCode();

        Map<String, Object> param = new HashMap<>(16);
        param.put("corpid", corpid);
        param.put("type", type);
        param.put("subType", subType);
        List<PushSetEntity> pushSetList = pushSetModel.findEntitys(param);

        PushSetEntity pushSetEntity;
        if (pushSetList.isEmpty()) {
            pushSetEntity = new PushSetEntity();
            pushSetEntity.setType(type);
            pushSetEntity.setSubType(subType);
            pushSetEntity.setEnable(0);

            JSONObject pushObject = new JSONObject();
            //type
            JSONArray typeArray = new JSONArray();
            JSONObject object = new JSONObject();
            object.put("name", PushObjectTypeEnum.MAINUSER.getName());
            object.put("alias", PushObjectTypeEnum.MAINUSER.getAlias());
            object.put("enable", 1);
            typeArray.add(object);
            JSONObject object1 = new JSONObject();
            object1.put("name", PushObjectTypeEnum.COLLABORATOR.getName());
            object1.put("alias", PushObjectTypeEnum.COLLABORATOR.getAlias());
            object1.put("enable", 0);
            typeArray.add(object1);
            JSONObject object2 = new JSONObject();
            object2.put("name", PushObjectTypeEnum.CONTRACT_SIGNING.getName());
            object2.put("alias", PushObjectTypeEnum.CONTRACT_SIGNING.getAlias());
            object2.put("enable", 1);
            typeArray.add(object2);
            pushObject.put("type", typeArray);
            //manager
            JSONObject managerObject = new JSONObject();
            managerObject.put("enable", 0);
            managerObject.put("value", new ArrayList<>());
            pushObject.put("manager", managerObject);
            //dep
            JSONObject depObject = new JSONObject();
            depObject.put("enable", 0);
            depObject.put("value", new ArrayList<>());
            pushObject.put("dep", depObject);
            //role
            JSONObject roleObject = new JSONObject();
            roleObject.put("enable", 0);
            roleObject.put("value", new ArrayList<>());
            pushObject.put("role", roleObject);
            //user
            JSONObject userObject = new JSONObject();
            userObject.put("enable", 0);
            userObject.put("value", new ArrayList<>());
            pushObject.put("user", userObject);

            pushSetEntity.setPushObject(pushObject.toJSONString());
        } else {
            pushSetEntity = pushSetList.get(0);
        }

        if (Objects.equals(PushTypeEnum.INVOICE_ADD_PUSH.getSubCode(), subType)) {
            pushSetEntity.setTitle(PushTypeEnum.INVOICE_ADD_PUSH.getName());
            pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.INVOICE_ADD));

        } else if (Objects.equals(PushTypeEnum.INVOICE_RED_ADD_PUSH.getSubCode(), subType)) {
            pushSetEntity.setTitle(PushTypeEnum.INVOICE_RED_ADD_PUSH.getName());
            pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.INVOICE_RED_ADD));

        } else if (Objects.equals(PushTypeEnum.INVOICE_DEL_PUSH.getSubCode(), subType)) {
            pushSetEntity.setTitle(PushTypeEnum.INVOICE_DEL_PUSH.getName());
            pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.INVOICE_DEL));

        } else if (Objects.equals(PushTypeEnum.INVOICE_REDUCTION.getSubCode(), subType)) {
            pushSetEntity.setTitle(PushTypeEnum.INVOICE_REDUCTION.getName());
            pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.INVOICE_REDUCTION));

        } else if (Objects.equals(PushTypeEnum.INVOICE_INVALIDATION.getSubCode(), subType)) {
            pushSetEntity.setTitle(PushTypeEnum.INVOICE_INVALIDATION.getName());
            pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.INVOICE_INVALIDATION));

        }

        return pushSetEntity;
    }

    @Override
    public PushSetEntity getPushSet(String corpid, String attr, PushSetModel pushSetModel) {
        return null;
    }

    @Override
    public List<PushSetEntity> getPushSet(String corpid, List<String> attrList, PushSetModel pushSetModel, Long templateId) {
        return null;
    }

    @Override
    public JSONObject getTimeJson(JSONObject jsonObject) {
        return null;
    }
}
